package cn.meye11.crm.controller;

import cn.meye11.crm.common.RestResponse;
import cn.meye11.crm.domain.model.Cost;
import cn.meye11.crm.domain.query.CostQuery;
import cn.meye11.crm.service.CostService;
import jakarta.servlet.http.HttpSession;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * (Cost)表服务控制类
 *
 * @author makejava
 * @since 2024-01-03 14:41:34
 */
@Slf4j
@Controller
@RequestMapping("cost")
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class CostController {

    private final CostService costService;

    // 代码写到此区域
    @GetMapping("manage")
    public String manage(Model model, HttpSession session) {
        model.addAttribute("user", session.getAttribute("user"));
        return "statistical_analysis/cost";
    }

    /************************************* 请在上方编写新代码 **************************************/
    // 此区域为自动生成的通用代码 尽量不直接修改
    @PostMapping("save")
    @ResponseBody
    public RestResponse<Long> save(@RequestBody Cost model) {
        try {
            Long id = costService.save(model);
            return RestResponse.success("成功", id);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResponse.failed(e.getMessage());
        }
    }

    @DeleteMapping("remove/{id}")
    @ResponseBody
    public RestResponse<String> remove(Cost model) {
        try {
            costService.remove(model);
            return RestResponse.success("成功");
        } catch (Exception e) {
            e.printStackTrace();
            return RestResponse.failed(e.getMessage());
        }
    }

    @PostMapping("update")
    @ResponseBody
    public RestResponse<String> update(@RequestBody Cost model) {
        try {
            if (model.getId() == null) {
                throw new RuntimeException("ID不能为空");
            }

            costService.update(model);
            return RestResponse.success("成功");
        } catch (Exception e) {
            e.printStackTrace();
            return RestResponse.failed(e.getMessage());
        }
    }

    @GetMapping("get/{id}")
    @ResponseBody
    public RestResponse<Cost> get(@PathVariable Long id) {
        Cost model = costService.get(id);
        return model == null ? RestResponse.failed("不存在") : RestResponse.success("成功", model);
    }


    public RestResponse<Long> count(CostQuery query) {
        Long count = costService.count(query);
        return RestResponse.success("成功", count);
    }

    @GetMapping("list")
    @ResponseBody
    public RestResponse<List<Cost>> list(CostQuery query) {
        try {
            return costService.list(query);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResponse.failed(e.getMessage());
        }
    }

    @GetMapping("page")
    @ResponseBody
    public RestResponse<List<Cost>> page(CostQuery query) {
        try {
            return costService.page(query);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResponse.failed(e.getMessage());
        }
    }
}
